import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:graphql/client.dart';

import 'ecgpacket.dart';
import 'gqlclient.dart';

class EcgData with ChangeNotifier {
  Stream<QueryResult> connectECGRecord(String rd) {
    // print('connectECGRecord:{$rd}');
    GQLClient gqlclient = GQLClient(
      gqladdress: 'https://apiv1.tianfulei.cn/recover/graphql',
      websocketEndpoint: 'wss://apiv1.tianfulei.cn/recoverwss/graphql',
      token: '7418148a-1be2-41c5-89e9-1463f3d56d8c',
    );

    var ret = gqlclient.listenRecordPacket(rd, (result) {
      // print('result:${result.data!["ecgRecordMonitor"]["sq"]}');
    });
    return ret;
  }

  Stream<EcgPacket> connectDecodedECGRecord(String rd) {
    print('connectDecodedECGRecord:$rd');
    var originalStream = connectECGRecord(rd);
    return originalStream.transform(StreamTransformer.fromHandlers(
        handleData: (QueryResult value, EventSink<EcgPacket> sink) {
      if (value.data != null) {
        var p = EcgPacket.fromJson(
            json.decode(value.data!["ecgRecordMonitor"]["pk"]));
        // print(value.data!["ecgRecordMonitor"]["sn"]);
        // print('sq:${p.sq}');
        // print('ecg len:${p.pk.ecg[0].wv.I.length}');
        // print('spo2 len:${p.pk.spo2[0].wave.length}');
        // p.pk.ecg[0].wv.calc();
        sink.add(p);
      }
    }));
  }

  Stream<QueryResult> connectECG(String sn) {
    GQLClient gqlclient = GQLClient(
      gqladdress: 'https://apiv1.tianfulei.cn/recover/graphql',
      websocketEndpoint: 'wss://apiv1.tianfulei.cn/recoverwss/graphql',
      token: '7418148a-1be2-41c5-89e9-1463f3d56d8c',
    );

    var ret = gqlclient.listenMachinePacket(sn, (result) {
      // print('result:${result.data!["ecgMachineMonitor"]["sq"]}');
    });
    return ret;
  }

  Stream<EcgPacket> connectDecodedECG(String sn) {
    var originalStream = connectECG(sn);
    return originalStream.transform(StreamTransformer.fromHandlers(
        handleData: (QueryResult value, EventSink<EcgPacket> sink) {
      if (value.data != null) {
        var p = EcgPacket.fromJson(
            json.decode(value.data!["ecgMachineMonitor"]["pk"]));
        // print(value.data!["ecgMachineMonitor"]["pk"]);
        // print('sq:${p.sq}');
        // print('ecg len:${p.pk.ecg[0].wv.I.length}');
        // print('spo2 len:${p.pk.spo2[0].wave.length}');
        // p.pk.ecg[0].wv.calc();
        sink.add(p);
      }
    }));
  }

  List<Ecg> repackEcg(Ecg ecg, int num) {
    List<Ecg> ecgs = [];
    int ecgLength = ecg.wv.I.length;
    int ecgDleta = ecg.wv.I.length ~/ num;
    // print('repack:$num,$ecgDleta');
    for (var i = 0; i < num; i++) {
      int start = i * ecgDleta;
      int end = (i + 1) * ecgDleta > ecgLength ? ecgLength : (i + 1) * ecgDleta;
      Wv wv = Wv(
          I: ecg.wv.I.sublist(start, end),
          II: ecg.wv.II.isNotEmpty ? ecg.wv.II.sublist(start, end) : ecg.wv.II,
          V1: ecg.wv.V1.isNotEmpty ? ecg.wv.V1.sublist(start, end) : ecg.wv.V1,
          V2: ecg.wv.V2.isNotEmpty ? ecg.wv.V2.sublist(start, end) : ecg.wv.V2,
          V3: ecg.wv.V3.isNotEmpty ? ecg.wv.V3.sublist(start, end) : ecg.wv.V3,
          V4: ecg.wv.V4.isNotEmpty ? ecg.wv.V4.sublist(start, end) : ecg.wv.V4,
          V5: ecg.wv.V5.isNotEmpty ? ecg.wv.V5.sublist(start, end) : ecg.wv.V5,
          V6: ecg.wv.V6.isNotEmpty ? ecg.wv.V6.sublist(start, end) : ecg.wv.V6,
          III: ecg.wv.III.isNotEmpty
              ? ecg.wv.III.sublist(start, end)
              : ecg.wv.III,
          AVR: ecg.wv.AVR.isNotEmpty
              ? ecg.wv.AVR.sublist(start, end)
              : ecg.wv.AVR,
          AVL: ecg.wv.AVL.isNotEmpty
              ? ecg.wv.AVL.sublist(start, end)
              : ecg.wv.AVL,
          AVF: ecg.wv.AVF.isNotEmpty
              ? ecg.wv.AVF.sublist(start, end)
              : ecg.wv.AVF);
      ecgs.add(Ecg(did: ecg.did, hr: ecg.hr, en: ecg.did, wv: wv));
    }
    return ecgs;
  }

  List<Spo2> repackSpo2(Spo2 spo2, int num) {
    List<Spo2> spo2s = [];
    int spo2Length = spo2.wave.length;
    int spo2Dleta = spo2.wave.length ~/ num;
    for (var i = 0; i < num; i++) {
      int start = i * spo2Dleta;
      int end =
          (i + 1) * spo2Dleta > spo2Length ? spo2Length : (i + 1) * spo2Dleta;
      List<int> wave = spo2.wave.sublist(start, end);
      // List<int> pulse = spo2.pulse.sublist(start, end);
      // List<int> signal = spo2.signal.sublist(start, end);
      // List<int> rdetected = spo2.rdetected.sublist(start, end);
      spo2s.add(Spo2(
          did: spo2.did,
          wave: wave,
          // pulse: pulse,
          // signal: signal,
          // rdetected: rdetected,
          // dropped: spo2.dropped,
          // decline: spo2.decline,
          // searchlong: spo2.searchlong,
          // searchingfluctuation: spo2.searchingfluctuation,
          // detectorerror: spo2.detectorerror,
          // searchingpulse: spo2.searchingpulse,
          spo2value: spo2.spo2value));
    }
    return spo2s;
  }

  List<Resp> repackResp(Resp resp, int num) {
    List<Resp> resps = [];
    int respLength = resp.wave.length;
    int respDleta = resp.wave.length ~/ num;
    for (var i = 0; i < num; i++) {
      int start = i * respDleta;
      int end =
          (i + 1) * respDleta > respLength ? respLength : (i + 1) * respDleta;
      List<int> wave = resp.wave.sublist(start, end);
      // List<int> xi = resp.xi.sublist(start, end);
      // List<int> hu = resp.hu.sublist(start, end);
      resps.add(Resp(
        did: resp.did,
        wave: wave,
        // xi: xi,
        // hu: hu,
        rate: resp.rate,
        // invalid: resp.invalid,
        // dropped: resp.dropped,
        // alarm: resp.alarm,
        // amplify: resp.amplify,
        // srcid: resp.srcid,
      ));
    }
    return resps;
  }

  List<EcgPacket> repackEcgPacket(EcgPacket pkt) {
    List<EcgPacket> pkts = [];

    int num = 10;

    List<Ecg> ecgs = [];
    if (pkt.pk.ecg.isEmpty) {
      return pkts;
    }

    ecgs = repackEcg(pkt.pk.ecg[0], num);

    List<Spo2> spo2s = [];
    if (pkt.pk.spo2.isNotEmpty) {
      spo2s = repackSpo2(pkt.pk.spo2[0], num);
    }

    List<Resp> resps = [];
    if (pkt.pk.resp.isNotEmpty) {
      resps = repackResp(pkt.pk.resp[0], num);
    }

    for (var i = 0; i < num; i++) {
      Pk pk = Pk(
        ecg: pkt.pk.ecg.isNotEmpty ? [ecgs[i]] : [],
        spo2: pkt.pk.spo2.isNotEmpty ? [spo2s[i]] : [],
        pulse: [],
        resp: pkt.pk.resp.isNotEmpty ? [resps[i]] : [],
        temp: pkt.pk.temp,
        nibp: pkt.pk.nibp,
      );

      EcgPacket p = EcgPacket(
          tp: pkt.tp,
          pk: pk,
          mc: pkt.mc,
          sn: pkt.sn,
          pt: pkt.pt,
          rd: pkt.rd,
          sq: pkt.sq);

      pkts.add(p);
    }
    return pkts;
  }

  int averageArriveTime(List<int> tm) {
    int totalTime = 0;
    // print('arrive time len:${tm.length}');
    for (var i = 0; i < tm.length; i++) {
      totalTime += tm[i];
    }
    return totalTime ~/ tm.length;
  }

  int averageArriveTotalTime(List<int> tm) {
    int totalTime = 0;
    // print('arrive time len:${tm.length}');
    for (var i = 0; i < tm.length; i++) {
      totalTime += tm[i];
    }
    return totalTime;
  }

  Stream<EcgPacket> connectRepackDecodedECG(String sn, String rd) async* {
    var lastTimestamp = DateTime.now().millisecondsSinceEpoch;
    List<EcgPacket> _ecg_packets = [];

    // List<int> _packetArriveMs = [];
    // const int cachedPacketCount = 10;
    Stream<EcgPacket> originalStream;
    if (rd != "") {
      originalStream = connectDecodedECGRecord(rd);
    } else {
      originalStream = connectDecodedECG(sn);
    }
    originalStream.listen((event) {
      _ecg_packets.addAll(repackEcgPacket(event));
      // 丢掉10秒以上的数据包
      const maxPacketNum = 60;
      if (_ecg_packets.length > maxPacketNum) {
        _ecg_packets = _ecg_packets.sublist(_ecg_packets.length - maxPacketNum);
      }
      // print(_other_packets);
    });

    int delayMs = 100;
    while (true) {
      if (_ecg_packets.isNotEmpty) {
        delayMs = 2000 ~/ _ecg_packets.length;
      }

      // print('${_ecg_packets.length}, $delayMs');

      await Future.delayed(Duration(milliseconds: delayMs));
      if (_ecg_packets.isNotEmpty) {
        var p = _ecg_packets.removeAt(0);
        yield p;
      }
    }
  }

  EcgPacket emptyEcgPacket() {
    return EcgPacket.fromJson(json.decode(
        '{"tp":"dat","pk":{"ecg":[{"did":0,"hr":0,"en":12,"wv":{"I":[],"II":[],"V1":[],"V2":[],"V3":[],"V4":[],"V5":[],"V6":[]}}],"spo2":[{"did":0,"wave":[],"pulse":[],"signal":[],"rdetected":[],"dropped":0,"decline":0,"searchlong":0,"searchingfluctuation":0,"detectorerror":0,"searchingpulse":0,"spo2value":0}],"pulse":[{"pid":11}],"resp":[{"did":4,"wave":[],"xi":[],"hu":[],"rate":0,"invalid":0,"dropped":0,"alarm":1,"amplify":1,"srcid":0}],"temp":[{"did":50,"l1error":0,"l2error":0,"detectorerror":1,"tempvalue":0},{"did":51,"l1error":0,"l2error":0,"detectorerror":0,"tempvalue":0}],"nibp":[{"did":3,"workmode":0,"measuremode":1,"working":0,"haveresult":1,"newresult":0,"measureInterval":0,"error":0,"errorcode":0,"sys":0,"map":0,"dia":0}]},"mc":"blta3","sn":"sn123456","pt":"wxx","rd":""}'));
  }

  Stream<String> simulateECG() async* {
    List<String> ss = [
      '{"tp":"dat","pk":{"ecg":[{"did":0,"hr":80,"en":12,"wv":{"I":[8209,8211,8214,8221,8227,8227,8229,8236,8238,8235,8230,8231,8230,8222,8217,8215,8212,8202,8191,8185,8177,8168,8162,8161,8161,8156,8155,8158,8159,8158,8156,8160,8163,8159,8159,8162,8165,8162,8160,8163,8165,8163,8163,8165,8168,8164,8164,8167,8168,8167,8166,8169,8171,8167,8167,8169,8172,8169,8166,8170,8171,8169,8168,8170,8173,8170,8170,8172,8173,8173,8171,8174,8176,8172,8171,8173,8176,8174,8172,8176,8176,8174,8175,8176,8179,8176,8175,8179,8183,8188,8193,8201,8209,8209,8213,8218,8221,8220,8216,8218,8216,8209,8204,8200,8197,8187,8180,8178,8176,8174,8172,8176,8178,8174,8174,8175,8177,8176,8174,8177,8179,8176,8175,8177,8179,8175],"II":[8224,8230,8238,8243,8247,8253,8260,8265,8267,8267,8265,8261,8257,8249,8241,8234,8225,8215,8200,8188,8174,8162,8154,8148,8140,8138,8137,8138,8139,8140,8141,8142,8143,8143,8143,8144,8145,8146,8146,8147,8148,8149,8149,8149,8150,8151,8151,8151,8151,8152,8155,8155,8154,8153,8154,8155,8156,8157,8157,8156,8157,8157,8157,8157,8159,8160,8160,8160,8160,8161,8162,8162,8162,8162,8162,8164,8164,8163,8164,8163,8166,8166,8166,8167,8166,8167,8168,8169,8176,8188,8200,8209,8219,8225,8232,8237,8239,8239,8238,8234,8230,8223,8216,8208,8199,8188,8177,8168,8163,8162,8162,8164,8166,8165,8165,8166,8167,8168,8167,8167,8168,8167,8168,8167,8167,8168],"V1":[8219,8218,8222,8226,8228,8229,8228,8236,8234,8235,8233,8233,8233,8228,8228,8226,8224,8222,8220,8214,8207,8197,8196,8192,8189,8184,8184,8186,8184,8183,8184,8187,8187,8184,8184,8187,8183,8185,8184,8187,8184,8184,8187,8188,8187,8185,8184,8188,8186,8185,8187,8187,8189,8185,8187,8188,8187,8187,8188,8188,8189,8184,8188,8188,8187,8187,8185,8190,8187,8187,8188,8191,8190,8187,8187,8191,8187,8189,8188,8190,8188,8186,8189,8189,8189,8187,8189,8191,8191,8192,8199,8206,8216,8218,8225,8229,8231,8234,8234,8237,8231,8226,8224,8221,8215,8206,8199,8196,8186,8183,8181,8183,8184,8182,8184,8186,8182,8182,8183,8185,8185,8181,8186,8184,8185,8180],"V2":[8257,8257,8268,8276,8282,8286,8293,8300,8300,8300,8299,8298,8298,8291,8287,8282,8271,8262,8251,8237,8218,8201,8193,8184,8177,8170,8166,8168,8165,8163,8165,8167,8169,8166,8168,8170,8167,8169,8169,8170,8169,8167,8171,8172,8171,8170,8171,8175,8172,8172,8172,8174,8175,8171,8174,8175,8174,8175,8175,8178,8177,8174,8177,8177,8176,8175,8174,8178,8176,8176,8178,8179,8180,8177,8179,8180,8177,8179,8178,8180,8179,8176,8179,8180,8179,8178,8179,8183,8181,8184,8192,8199,8207,8209,8216,8221,8223,8225,8226,8230,8226,8220,8219,8215,8209,8199,8193,8190,8181,8176,8175,8175,8177,8175,8178,8179,8176,8178,8176,8179,8178,8175,8180,8180,8179,8176],"V3":[8270,8274,8285,8297,8307,8312,8320,8331,8336,8338,8338,8337,8335,8326,8319,8308,8294,8280,8261,8239,8211,8187,8169,8157,8146,8134,8130,8132,8129,8129,8130,8133,8135,8132,8134,8137,8136,8137,8138,8140,8139,8138,8141,8142,8142,8141,8142,8147,8145,8144,8147,8148,8148,8146,8149,8152,8151,8151,8151,8153,8153,8151,8153,8153,8154,8152,8154,8158,8156,8156,8158,8160,8160,8157,8160,8161,8160,8159,8159,8161,8160,8159,8162,8165,8164,8162,8163,8167,8166,8172,8183,8198,8208,8213,8222,8228,8231,8234,8234,8236,8233,8226,8223,8218,8211,8199,8190,8180,8167,8160,8159,8160,8161,8160,8163,8163,8163,8163,8164,8167,8165,8162,8165,8167,8167,8165],"V4":[8241,8253,8262,8269,8277,8283,8292,8293,8293,8295,8293,8290,8282,8276,8271,8256,8246,8231,8212,8192,8168,8159,8149,8139,8132,8127,8130,8128,8127,8133,8132,8134,8132,8134,8137,8133,8135,8138,8139,8141,8137,8142,8143,8141,8141,8142,8145,8144,8142,8147,8147,8148,8146,8147,8151,8147,8148,8149,8152,8153,8148,8152,8154,8152,8153,8151,8156,8154,8154,8159,8157,8159,8156,8158,8162,8157,8158,8159,8160,8160,8157,8162,8162,8161,8162,8161,8167,8168,8173,8187,8198,8208,8215,8222,8230,8231,8234,8236,8236,8234,8225,8223,8219,8208,8197,8186,8177,8166,8158,8161,8160,8161,8160,8163,8165,8160,8162,8164,8166,8165,8161,8166,8166,8164,8165,8164],"V5":[8215,8218,8226,8234,8241,8246,8253,8262,8263,8265,8263,8262,8261,8256,8254,8247,8237,8228,8216,8204,8188,8170,8162,8153,8147,8137,8134,8137,8134,8133,8136,8139,8141,8137,8141,8143,8142,8142,8142,8145,8144,8143,8146,8147,8147,8146,8147,8152,8148,8149,8149,8151,8152,8150,8153,8154,8153,8153,8154,8157,8156,8154,8157,8158,8159,8156,8156,8162,8159,8160,8161,8163,8163,8160,8163,8164,8162,8163,8163,8166,8164,8161,8164,8166,8166,8165,8165,8170,8169,8175,8186,8200,8211,8216,8226,8233,8236,8238,8239,8241,8237,8232,8229,8223,8214,8202,8193,8183,8170,8163,8161,8162,8164,8163,8165,8166,8164,8164,8165,8168,8168,8164,8168,8168,8169,8166],"V6":[8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192]}}],"spo2":[{"did":0,"wave":[127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127],"pulse":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"signal":[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],"rdetected":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"dropped":0,"decline":0,"searchlong":0,"searchingfluctuation":0,"detectorerror":0,"searchingpulse":0,"spo2value":127}],"pulse":[{"pid":11}],"resp":[{"did":4,"wave":[128,128,128,129,129,129,129,130,130,130,130,130,130,130,130,130,130,129,129,129,129,129,129,129,129,129,129,129,129,129,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,127,127,127,126,126],"xi":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"hu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"rate":0,"invalid":0,"dropped":0,"alarm":1,"amplify":1,"srcid":0}],"temp":[{"did":50,"l1error":0,"l2error":0,"detectorerror":1,"tempvalue":8191},{"did":51,"l1error":0,"l2error":0,"detectorerror":0,"tempvalue":257}],"nibp":[{"did":3,"workmode":0,"measuremode":1,"working":0,"haveresult":1,"newresult":0,"measureInterval":0,"error":0,"errorcode":0,"sys":940,"map":650,"dia":510}],"bp":[],"co2":[],"o2":[],"n2o":[],"aa":[],"icg":[],"alarm":null},"mc":"blta3","sn":"sn123456","pt":"wxx","rd":""}',
      // '{"tp":"dat","pk":{"ecg":[{"did":0,"hr":80,"en":12,"wv":{"I":[8176,8176,8165,8147,8134,8153,8207,8281,8375,8472,8556,8583,8546,8474,8384,8285,8196,8145,8132,8132,8140,8152,8158,8158,8157,8157,8157,8154,8156,8158,8158,8156,8158,8165,8167,8163,8164,8167,8171,8170,8168,8174,8177,8177,8180,8184,8186,8184,8186,8190,8190,8188,8189,8195,8197,8195,8196,8200,8203,8203,8203,8210,8213,8214,8218,8224,8230,8229,8231,8237,8237,8233,8228,8230,8227,8219,8216,8212,8207,8196,8186,8179,8171,8163,8158,8159,8160,8155,8156,8160,8160,8159,8158,8162,8162,8159,8161,8163,8165,8162,8162,8166,8165,8162,8162,8166,8168,8164,8165,8168,8168,8166,8165,8169,8170,8167,8168,8169,8171,8168,8168,8171,8170],"II":[8168,8163,8149,8129,8114,8140,8218,8331,8463,8598,8715,8752,8695,8579,8440,8290,8155,8072,8055,8074,8101,8126,8142,8148,8146,8141,8140,8144,8147,8147,8146,8147,8151,8154,8157,8155,8155,8159,8162,8166,8166,8169,8175,8178,8182,8183,8184,8186,8190,8192,8192,8192,8196,8199,8203,8204,8206,8209,8210,8215,8219,8223,8229,8235,8242,8248,8252,8256,8259,8264,8266,8264,8261,8255,8250,8242,8235,8229,8219,8206,8191,8175,8164,8155,8148,8143,8140,8138,8138,8140,8140,8141,8142,8141,8142,8142,8143,8146,8145,8145,8147,8147,8148,8147,8147,8147,8149,8151,8151,8152,8153,8153,8154,8154,8155,8154,8154,8155,8155,8156,8157,8157,8158],"V1":[8183,8185,8186,8182,8187,8185,8192,8203,8225,8253,8277,8303,8306,8262,8186,8090,7994,7899,7844,7873,7951,8059,8148,8196,8214,8208,8198,8194,8198,8204,8199,8195,8198,8201,8207,8203,8203,8201,8203,8204,8205,8204,8205,8207,8213,8209,8206,8205,8206,8213,8209,8210,8212,8214,8214,8210,8213,8217,8214,8217,8217,8221,8220,8218,8227,8227,8229,8229,8233,8237,8235,8232,8235,8233,8233,8226,8228,8226,8221,8220,8213,8211,8202,8195,8194,8191,8186,8185,8182,8186,8181,8183,8185,8184,8184,8181,8186,8188,8185,8187,8187,8189,8188,8184,8188,8185,8187,8186,8187,8189,8184,8185,8188,8187,8188,8185,8188,8191,8186,8189,8187,8190,8187],"V2":[8177,8180,8179,8172,8171,8164,8171,8200,8249,8309,8365,8423,8432,8354,8217,8050,7875,7706,7609,7653,7786,7962,8114,8196,8227,8217,8200,8195,8201,8212,8208,8200,8204,8210,8217,8213,8211,8211,8214,8221,8218,8217,8217,8219,8229,8228,8227,8228,8232,8240,8236,8234,8238,8242,8246,8243,8247,8250,8250,8252,8254,8260,8260,8263,8274,8279,8285,8291,8295,8301,8299,8297,8299,8297,8295,8286,8282,8275,8262,8252,8238,8224,8206,8191,8188,8179,8171,8167,8164,8167,8164,8165,8167,8168,8169,8165,8168,8171,8168,8169,8169,8172,8171,8168,8174,8172,8172,8171,8171,8175,8172,8172,8174,8174,8174,8171,8175,8178,8175,8176,8174,8177,8176],"V3":[8166,8169,8165,8155,8148,8136,8150,8212,8314,8438,8562,8679,8727,8651,8486,8278,8064,7852,7719,7727,7826,7965,8086,8158,8186,8180,8164,8157,8166,8176,8172,8167,8169,8176,8182,8181,8181,8182,8188,8193,8196,8196,8196,8200,8210,8213,8213,8214,8222,8229,8229,8228,8233,8239,8243,8243,8248,8252,8255,8259,8263,8270,8275,8282,8296,8306,8312,8318,8327,8337,8338,8338,8337,8336,8331,8321,8310,8298,8282,8264,8244,8221,8194,8173,8161,8148,8138,8132,8129,8132,8128,8129,8132,8134,8134,8132,8136,8139,8137,8137,8137,8140,8140,8140,8143,8142,8143,8143,8145,8148,8145,8146,8148,8149,8150,8146,8149,8151,8150,8151,8152,8155,8153],"V4":[8167,8161,8144,8131,8114,8142,8233,8363,8520,8672,8809,8855,8770,8614,8421,8222,8041,7932,7928,7980,8049,8110,8139,8156,8149,8138,8138,8143,8153,8149,8146,8152,8156,8162,8159,8159,8162,8163,8169,8171,8172,8174,8177,8188,8188,8187,8190,8193,8200,8199,8200,8207,8209,8214,8213,8217,8222,8220,8228,8232,8237,8243,8247,8261,8268,8273,8282,8287,8293,8293,8292,8295,8291,8287,8277,8269,8262,8247,8235,8218,8198,8178,8159,8153,8141,8131,8130,8128,8131,8128,8129,8134,8132,8133,8131,8134,8138,8134,8137,8137,8139,8141,8139,8143,8140,8139,8142,8143,8146,8144,8145,8148,8145,8147,8145,8149,8151,8147,8150,8150,8152,8153,8150],"V5":[8167,8170,8163,8146,8126,8100,8110,8183,8301,8449,8598,8741,8817,8780,8663,8504,8341,8179,8064,8029,8044,8081,8112,8131,8146,8144,8138,8133,8139,8149,8147,8147,8148,8152,8158,8157,8160,8160,8160,8165,8167,8169,8168,8170,8179,8181,8183,8183,8186,8192,8191,8192,8195,8197,8201,8201,8205,8207,8207,8209,8211,8217,8219,8222,8232,8239,8245,8251,8257,8263,8262,8262,8263,8262,8259,8252,8248,8240,8229,8220,8206,8194,8177,8161,8156,8146,8140,8137,8135,8137,8134,8135,8138,8140,8140,8136,8140,8143,8141,8142,8143,8146,8145,8144,8147,8147,8148,8147,8148,8152,8149,8149,8151,8152,8152,8150,8155,8156,8155,8155,8155,8158,8157],"V6":[8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192]}}],"spo2":[{"did":0,"wave":[127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127],"pulse":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"signal":[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],"rdetected":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"dropped":0,"decline":0,"searchlong":0,"searchingfluctuation":0,"detectorerror":0,"searchingpulse":0,"spo2value":127}],"pulse":[{"pid":11}],"resp":[{"did":4,"wave":[126,125,125,125,125,125,124,124,124,124,124,124,123,123,123,123,123,124,124,124,124,125,125,125,126,126,127,127,128,128,128,128,129,129,129,129,129,129,129,129,129,129,129,128,128,128,128,128,128,128],"xi":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"hu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"rate":0,"invalid":0,"dropped":0,"alarm":1,"amplify":1,"srcid":0}],"temp":[{"did":50,"l1error":0,"l2error":0,"detectorerror":1,"tempvalue":8191},{"did":51,"l1error":0,"l2error":0,"detectorerror":0,"tempvalue":257}],"nibp":[{"did":3,"workmode":0,"measuremode":1,"working":0,"haveresult":1,"newresult":0,"measureInterval":0,"error":0,"errorcode":0,"sys":940,"map":650,"dia":510}],"bp":[],"co2":[],"o2":[],"n2o":[],"aa":[],"icg":[],"alarm":null},"mc":"blta3","sn":"sn123456","pt":"wxx","rd":""}',
      // '{"tp":"dat","pk":{"ecg":[{"did":0,"hr":80,"en":12,"wv":{"I":[8176,8176,8165,8147,8134,8153,8207,8281,8375,8472,8556,8583,8546,8474,8384,8285,8196,8145,8132,8132,8140,8152,8158,8158,8157,8157,8157,8154,8156,8158,8158,8156,8158,8165,8167,8163,8164,8167,8171,8170,8168,8174,8177,8177,8180,8184,8186,8184,8186,8190,8190,8188,8189,8195,8197,8195,8196,8200,8203,8203,8203,8210,8213,8214,8218,8224,8230,8229,8231,8237,8237,8233,8228,8230,8227,8219,8216,8212,8207,8196,8186,8179,8171,8163,8158,8159,8160,8155,8156,8160,8160,8159,8158,8162,8162,8159,8161,8163,8165,8162,8162,8166,8165,8162,8162,8166,8168,8164,8165,8168,8168,8166,8165,8169,8170,8167,8168,8169,8171,8168,8168,8171,8170],"II":[8168,8163,8149,8129,8114,8140,8218,8331,8463,8598,8715,8752,8695,8579,8440,8290,8155,8072,8055,8074,8101,8126,8142,8148,8146,8141,8140,8144,8147,8147,8146,8147,8151,8154,8157,8155,8155,8159,8162,8166,8166,8169,8175,8178,8182,8183,8184,8186,8190,8192,8192,8192,8196,8199,8203,8204,8206,8209,8210,8215,8219,8223,8229,8235,8242,8248,8252,8256,8259,8264,8266,8264,8261,8255,8250,8242,8235,8229,8219,8206,8191,8175,8164,8155,8148,8143,8140,8138,8138,8140,8140,8141,8142,8141,8142,8142,8143,8146,8145,8145,8147,8147,8148,8147,8147,8147,8149,8151,8151,8152,8153,8153,8154,8154,8155,8154,8154,8155,8155,8156,8157,8157,8158],"V1":[8183,8185,8186,8182,8187,8185,8192,8203,8225,8253,8277,8303,8306,8262,8186,8090,7994,7899,7844,7873,7951,8059,8148,8196,8214,8208,8198,8194,8198,8204,8199,8195,8198,8201,8207,8203,8203,8201,8203,8204,8205,8204,8205,8207,8213,8209,8206,8205,8206,8213,8209,8210,8212,8214,8214,8210,8213,8217,8214,8217,8217,8221,8220,8218,8227,8227,8229,8229,8233,8237,8235,8232,8235,8233,8233,8226,8228,8226,8221,8220,8213,8211,8202,8195,8194,8191,8186,8185,8182,8186,8181,8183,8185,8184,8184,8181,8186,8188,8185,8187,8187,8189,8188,8184,8188,8185,8187,8186,8187,8189,8184,8185,8188,8187,8188,8185,8188,8191,8186,8189,8187,8190,8187],"V2":[8177,8180,8179,8172,8171,8164,8171,8200,8249,8309,8365,8423,8432,8354,8217,8050,7875,7706,7609,7653,7786,7962,8114,8196,8227,8217,8200,8195,8201,8212,8208,8200,8204,8210,8217,8213,8211,8211,8214,8221,8218,8217,8217,8219,8229,8228,8227,8228,8232,8240,8236,8234,8238,8242,8246,8243,8247,8250,8250,8252,8254,8260,8260,8263,8274,8279,8285,8291,8295,8301,8299,8297,8299,8297,8295,8286,8282,8275,8262,8252,8238,8224,8206,8191,8188,8179,8171,8167,8164,8167,8164,8165,8167,8168,8169,8165,8168,8171,8168,8169,8169,8172,8171,8168,8174,8172,8172,8171,8171,8175,8172,8172,8174,8174,8174,8171,8175,8178,8175,8176,8174,8177,8176],"V3":[8166,8169,8165,8155,8148,8136,8150,8212,8314,8438,8562,8679,8727,8651,8486,8278,8064,7852,7719,7727,7826,7965,8086,8158,8186,8180,8164,8157,8166,8176,8172,8167,8169,8176,8182,8181,8181,8182,8188,8193,8196,8196,8196,8200,8210,8213,8213,8214,8222,8229,8229,8228,8233,8239,8243,8243,8248,8252,8255,8259,8263,8270,8275,8282,8296,8306,8312,8318,8327,8337,8338,8338,8337,8336,8331,8321,8310,8298,8282,8264,8244,8221,8194,8173,8161,8148,8138,8132,8129,8132,8128,8129,8132,8134,8134,8132,8136,8139,8137,8137,8137,8140,8140,8140,8143,8142,8143,8143,8145,8148,8145,8146,8148,8149,8150,8146,8149,8151,8150,8151,8152,8155,8153],"V4":[8167,8161,8144,8131,8114,8142,8233,8363,8520,8672,8809,8855,8770,8614,8421,8222,8041,7932,7928,7980,8049,8110,8139,8156,8149,8138,8138,8143,8153,8149,8146,8152,8156,8162,8159,8159,8162,8163,8169,8171,8172,8174,8177,8188,8188,8187,8190,8193,8200,8199,8200,8207,8209,8214,8213,8217,8222,8220,8228,8232,8237,8243,8247,8261,8268,8273,8282,8287,8293,8293,8292,8295,8291,8287,8277,8269,8262,8247,8235,8218,8198,8178,8159,8153,8141,8131,8130,8128,8131,8128,8129,8134,8132,8133,8131,8134,8138,8134,8137,8137,8139,8141,8139,8143,8140,8139,8142,8143,8146,8144,8145,8148,8145,8147,8145,8149,8151,8147,8150,8150,8152,8153,8150],"V5":[8167,8170,8163,8146,8126,8100,8110,8183,8301,8449,8598,8741,8817,8780,8663,8504,8341,8179,8064,8029,8044,8081,8112,8131,8146,8144,8138,8133,8139,8149,8147,8147,8148,8152,8158,8157,8160,8160,8160,8165,8167,8169,8168,8170,8179,8181,8183,8183,8186,8192,8191,8192,8195,8197,8201,8201,8205,8207,8207,8209,8211,8217,8219,8222,8232,8239,8245,8251,8257,8263,8262,8262,8263,8262,8259,8252,8248,8240,8229,8220,8206,8194,8177,8161,8156,8146,8140,8137,8135,8137,8134,8135,8138,8140,8140,8136,8140,8143,8141,8142,8143,8146,8145,8144,8147,8147,8148,8147,8148,8152,8149,8149,8151,8152,8152,8150,8155,8156,8155,8155,8155,8158,8157],"V6":[8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192]}}],"spo2":[{"did":0,"wave":[127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127],"pulse":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"signal":[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],"rdetected":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"dropped":0,"decline":0,"searchlong":0,"searchingfluctuation":0,"detectorerror":0,"searchingpulse":0,"spo2value":127}],"pulse":[{"pid":11}],"resp":[{"did":4,"wave":[126,125,125,125,125,125,124,124,124,124,124,124,123,123,123,123,123,124,124,124,124,125,125,125,126,126,127,127,128,128,128,128,129,129,129,129,129,129,129,129,129,129,129,128,128,128,128,128,128,128],"xi":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"hu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"rate":0,"invalid":0,"dropped":0,"alarm":1,"amplify":1,"srcid":0}],"temp":[{"did":50,"l1error":0,"l2error":0,"detectorerror":1,"tempvalue":8191},{"did":51,"l1error":0,"l2error":0,"detectorerror":0,"tempvalue":257}],"nibp":[{"did":3,"workmode":0,"measuremode":1,"working":0,"haveresult":1,"newresult":0,"measureInterval":0,"error":0,"errorcode":0,"sys":940,"map":650,"dia":510}],"bp":[],"co2":[],"o2":[],"n2o":[],"aa":[],"icg":[],"alarm":null},"mc":"blta3","sn":"sn123456","pt":"wxx","rd":""}',
    ];
    var i = 0;
    while (true) {
      yield ss[i];
      i++;
      if (i >= ss.length) i = 0;
      await Future.delayed(const Duration(milliseconds: 5 * 100));
    }
  }

  Stream<EcgPacket> simulateDecodedECG() {
    var originalStream = simulateECG();
    var i = 0;
    return originalStream.transform(StreamTransformer.fromHandlers(
        handleData: (String value, EventSink<EcgPacket> sink) {
      var p = EcgPacket.fromJson(json.decode(value));
      p.sn += '${i++}';
      sink.add(p);
    }));
  }

  Stream<EcgPacket> simulatedRepackDecodedECG() async* {
    var lastTimestamp = DateTime.now().millisecondsSinceEpoch;
    List<EcgPacket> _packets = [];
    var originalStream = simulateDecodedECG();
    originalStream.listen((event) {
      // 上个包到这个包间隔
      var ns = DateTime.now().millisecondsSinceEpoch;
      var ds = ns - lastTimestamp;
      // print(ds);
      lastTimestamp = ns;
      _packets.add(event);
    });

    while (true) {
      await Future.delayed(const Duration(milliseconds: 10));
      if (_packets.isNotEmpty) {
        var p = _packets.removeAt(0);
        // print(
        // 'Len:Ecg:${p.pk.ecg[0].wv.I.length},SPO2:${p.pk.spo2[0].wave.length},RESP:${p.pk.resp[0].wave.length}');
        yield p;
      }
    }
  }
}
